package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.dto.api.ApiProductSearchDTO;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.product.ProductItemVO;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author LT
 * @create 2024-03-26-16:57
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Resource
    ProductService productService;
    @Resource
    ProductSkuService productSkuService;

    @PutMapping("/sku/batchUpdateSkuStockAndSale")
    public Result batchUpdateSkuStockAndSale(@RequestBody Map<Long, Integer> map){
        map.forEach((skuId,skuNum) ->{
            ProductSku productSku = productService.getBySkuId(skuId);
            productSku.setSaleNum(productSku.getSaleNum()+skuNum);
            productSku.setStockNum(productSku.getStockNum()-skuNum);
            productSkuService.updateSkuStockAndSaleById(productSku);
        });
        return Result.ok();
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum")Integer pageNum,
                       @PathVariable("pageSize")Integer pageSize,
                       ApiProductSearchDTO apiProductSearchDTO){
        PageInfo<ProductSku> page = productService.list(pageNum,pageSize,apiProductSearchDTO);
        return Result.ok().data(page);
    }

    @GetMapping("/item/{skuId}")
    public Result getItemBySkuId(@PathVariable("skuId")Long skuId){
       ProductItemVO productItemVO = productService.getItemBySkuId(skuId);
        return Result.ok(productItemVO);
    }

    @GetMapping("/getBySkuId/{skuId}")
    public Result<ProductSku> getBySkuId(@PathVariable("skuId")Long skuId){
        ProductSku productSku = productService.getBySkuId(skuId);
        return Result.ok(productSku);
    }


}
